package com.awspaas.user.apps.processforsap.impl.send;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.common.profile.StringUtil;
import com.awspaas.user.apps.processforsap.model.MessageHeader;
import com.awspaas.user.apps.processforsap.util.HttpClientUtil;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

import java.util.ArrayList;
import java.util.List;

public class SenderSD001HtImpl extends ParentInterface {

    public SenderSD001HtImpl(JSONObject jsonObject) {
        super(jsonObject);
    }

    @Override
    public void dueInterFaceData() {
        String result = "";
        String resultType = "S";
        MessageHeader messageHeader = super.messageHeader;
        JSONObject sendData = super.sendData;
        JSONObject resultSend =new JSONObject();
        String bindid = sendData.getString("BINDID");
        BO boMain = SDK.getBOAPI().query("BO_EU_SAPSD_KHSWXGXYPS").detailByBindId(bindid);
        JSONObject newJsonResult =new JSONObject();
        List<JSONObject> newList = new ArrayList<>();

        /**
         * 获取币种
         */
        String field0017 = boMain.getString("FIELD0017");
        List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_SWLIST").bindId(bindid).list();
        try {

            for (BO bo:list){
                JSONArray Requests  =  new JSONArray();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("KUNNR",bo.getString("CUSTCODE"));
//                jsonObject.put("TYPE","2");
//                jsonObject.put("KTOKD","Z001");
//                jsonObject.put("PARTNER_ROLE","ZXD01");
                String yxqyzt = bo.getString("YXQYZT");
                if (yxqyzt.contains("FT00")){
                    jsonObject.put("TITLE_LET","香港");
                }
                String[] yss = yxqyzt.split(",");
                JSONArray rows1= new JSONArray();
                JSONArray rows2= new JSONArray();

                for (String ys:yss){
                    JSONObject jsonObject0 = new JSONObject();
                    jsonObject0.put("BUKRS",ys);
                    //人民币，内销    美元，外销
                    if ("RMB".equals(field0017)){
                        jsonObject0.put("AKONT","2203010100");
                    }else{
                        jsonObject0.put("AKONT","2203010200");
                    }
                    jsonObject0.put("ZTERM",boMain.getString("FIELD0018"));
                    rows1.add(jsonObject0);


                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("VKORG",ys);
                    jsonObject1.put("VTWEG","00");
                    //根据区域关联
                    String field0014 = boMain.getString("FIELD0014");
                    if (field0014.equals("华东一部")){
                        jsonObject1.put("SPART","10");
                    }else if (field0014.equals("华南部")||field0014.equals("华北部")){
                        jsonObject1.put("SPART","20");
                    }else if (field0014.equals("台湾部")||field0014.equals("华东二部")){
                        jsonObject1.put("SPART","30");
                    }else if (field0014.equals("欧美部")){
                        jsonObject1.put("SPART","50");
                    }
                    jsonObject1.put("BZIRK","");
                    jsonObject1.put("KDGRP","");

                    if (field0014.equals("华东一部")){
                        jsonObject1.put("VKBUR","8001");
                    }else if (field0014.equals("华南部")){
                        jsonObject1.put("VKBUR","8003");
                    }else if (field0014.equals("台湾部")){
                        jsonObject1.put("VKBUR","8004");
                    }else if (field0014.equals("欧美部")){
                        jsonObject1.put("VKBUR","8006");
                    }else if (field0014.equals("华东二部")){
                        jsonObject1.put("VKBUR","8005");
                    }else if (field0014.equals("华北部")){
                        jsonObject1.put("VKBUR","8002");
                    }

                    jsonObject1.put("WAERS","RMB".equals(boMain.getString("FIELD0017"))?"CNY":"USD");

                    jsonObject1.put("KALKS","RMB".equals(boMain.getString("FIELD0017"))?"2":"1");

                    // 待确认  交货工厂
                    jsonObject1.put("VWERK",ys);

                    //结算方式：入甬矽库结算|出甬矽库结算|按终端结算（入甬矽库结算）|按终端结算（出甬矽库结算）|按型号结算
                    if ("入甬矽库结算".equals(boMain.getString("FIELD0008"))||"按终端结算（入甬矽库结算）".equals(boMain.getString("FIELD0008"))){
                        jsonObject1.put("VSBED","01");
                    }else{
                        jsonObject1.put("VSBED","02");
                    }
                    //贸易术语出口：FOB|CIF|DDU|DDP
                    jsonObject1.put("INCO1_CK",boMain.getString("FIELD0012"));
                    jsonObject1.put("INCO1_JK",boMain.getString("FIELD0065"));
                    //   客户科目分配组
                    jsonObject1.put("KTGRD","RMB".equals(boMain.getString("FIELD0017"))?"03":"04");
                    jsonObject1.put("ZTERM",boMain.getString("FIELD0018"));
                    jsonObject1.put("TATYP","MWST");

                    jsonObject1.put("TAXKD","RMB".equals(boMain.getString("FIELD0017"))?"1":"0");
                    rows2.add(jsonObject1);


                }

                jsonObject.put("Rows_Comp",rows1);

                jsonObject.put("Rows_Sales",rows2);

                //固定值
                JSONArray Rows_Partner = new JSONArray();
                JSONObject z1 = new JSONObject();
                z1.put("PARVW","Z1");
                z1.put("KTONR",bo.getString("CUSTCODE"));
                Rows_Partner.add(z1);


                JSONObject z2 = new JSONObject();
                z2.put("PARVW","Z3");
                z2.put("KTONR",boMain.getString("CREATEUSER"));
                Rows_Partner.add(z2);


                JSONObject z3 = new JSONObject();
                z3.put("PARVW","Z4");
                z3.put("KTONR",boMain.getString("FIELD0028"));
                Rows_Partner.add(z3);
                jsonObject.put("Rows_Partner",Rows_Partner);


                //S4
                JSONArray Rows_Credit = new JSONArray();
                JSONObject jsonObject222 = new JSONObject();
                jsonObject222.put("LIMIT_RULE","STANDARD");
                jsonObject222.put("RISK_CLASS","0");
                jsonObject222.put("CHECK_RULE","01");
                jsonObject222.put("SEGMENT","FHEC");
                jsonObject222.put("X_LIMIT_IS_SET","X");
                jsonObject222.put("CREDIT_LIMIT",bo.getString("FIELD0021"));
                Rows_Credit.add(jsonObject222);
                jsonObject.put("Rows_Credit",Rows_Credit);
                Requests.add(jsonObject);

                sendData.put("Requests",Requests);

                resultSend.put(bo.getString("CUSTCODE"),sendData);
                BO configBo = messageHeader.getConfigBo();
                String url = configBo.getString("SEND_URL");
                Object resultData11 = SDK.getProcessAPI().getVariable(bindid, messageHeader.getInterface_ID()+bo.getId());
                if (resultData11==null){
                    result = HttpClientUtil.doPost(url, sendData.toJSONString(), SDK.getRuleAPI().executeAtScript("@getSapParamas(PONAME)"), SDK.getRuleAPI().executeAtScript("@getSapParamas(POPASSWORD)"));
                    JSONObject jsonObjectR = JSONObject.parseObject(result);
                    JSONArray jsonArray = jsonObjectR.getJSONArray("Responses");
                    for (int i =0;i<jsonArray.size();i++){
                        JSONObject jsonObject1R = jsonArray.getJSONObject(i);
                        String msgtype= jsonObject1R.getString("MSGTYPE");
                        if (msgtype.equals("E")){
                            resultType = "E";
                        }else{
                            SDK.getProcessAPI().setVariable(bindid, messageHeader.getInterface_ID()+bo.getId(), true);
                        }
                        newList.add(jsonObject1R);
                    }
                }else {
                    resultType = "S";
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("MSG","客户三码："+bo.getString("CUSTCODE")+"已成功调用，此次跳过");
                    newList.add(jsonObject2);

                }
            }
            //封装最终数据
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("Type",resultType);
            jsonObject1.put("MESSAGE",newList);
            newJsonResult.put("Responses",jsonObject1);
            result = JSONObject.toJSONString(newJsonResult);
        }catch (Exception e) {
            e.printStackTrace();
            super.resultType = false;
            throw new BPMNError("0331",e.getMessage());
        } finally {
            super.resultSendData = resultSend.toJSONString();
            super.returnData = result;
        }

    }

}